//
//  CommentTableViewCell.m
//  项目三
//
//  Created by rimi1 on 2016/12/12.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "CommentTableViewCell.h"

static NSInteger spacing_ = 10;

@implementation CommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.headImage];
        [self.contentView addSubview:self.name];
        [self.contentView addSubview:self.content];
        [self.contentView addSubview:self.date];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect rect = self.content.frame;
    CGFloat height = [CommentTableViewCell heightWithText:self.content.text];
    rect.size.height = height;
    self.content.frame = rect;
    
    CGRect dateRect = self.date.frame;
    dateRect.origin.y = CGRectGetMaxY(self.content.frame) + spacing_/2;
    self.date.frame = dateRect;
}

+ (CGFloat)heightWithText:(NSString *)text {
    CGFloat height = [text boundingRectWithSize:CGSizeMake(kWIDTH - 2 * spacing_, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:WXQAdaptionHeight(15)]} context:nil].size.height;
    
    return height;
}

+ (CGFloat)cellHeightWithText:(NSString *)text {
    CGFloat contentHeight = [CommentTableViewCell heightWithText:text];
    CGFloat height = spacing_ + WXQAdaptionHeight(30) + spacing_ + contentHeight + spacing_/2 + WXQAdaptionHeight(13) + spacing_;
    return height;
}

+ (NSString *)strWithDate:(NSDate *)date {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy.MM.dd HH:MM";
    
    return [format stringFromDate:date];
}

#pragma mark - respondsTo..

- (void)respondsToHead {
    if (self.tapHead) {
        self.tapHead(self);
    }
}

#pragma mark - set

- (void)setObj:(AVObject *)obj {
    _obj = obj;
    
    NSURL *url = [NSURL URLWithString:obj[@"user"][@"headImage"]];
    [self.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"个人"] options:SDWebImageProgressiveDownload];
    self.name.text = obj[@"user"][@"name"];
    self.content.text = obj[@"content"];
    self.date.text = [CommentTableViewCell strWithDate:obj[@"createdAt"]];
}

#pragma mark - get

- (UIImageView *)headImage {
    
    if (!_headImage) {
        
        CGFloat diameter = WXQAdaptionHeight(30);
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(spacing_, spacing_, diameter, diameter)];
        _headImage.layer.cornerRadius = diameter/2;
        _headImage.layer.masksToBounds = YES;
        _headImage.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToHead)];
        [_headImage addGestureRecognizer:tap];
        
    }
    return _headImage;
}

- (UILabel *)name {
    
    if (!_name) {
        
        CGFloat x = CGRectGetMaxX(self.headImage.frame) + spacing_;
        _name = [[UILabel alloc] initWithFrame:CGRectMake(x, CGRectGetMinY(self.headImage.frame), kWIDTH - x - spacing_, CGRectGetHeight(self.headImage.bounds))];
        _name.font = [UIFont systemFontOfSize:WXQAdaptionHeight(13)];
        
    }
    return _name;
}

- (UILabel *)content {
    
    if (!_content) {
        
        _content = [[UILabel alloc] initWithFrame:CGRectMake(spacing_, CGRectGetMaxY(self.headImage.frame) + spacing_, kWIDTH - 2 * spacing_, 0)];
        _content.numberOfLines = 0;
        _content.font = [UIFont systemFontOfSize:WXQAdaptionHeight(15)];
        
    }
    return _content;
}

- (UILabel *)date {
    
    if (!_date) {
        
        CGFloat height = WXQAdaptionHeight(13);
        _date = [[UILabel alloc] initWithFrame:CGRectMake(spacing_, 0, kWIDTH - 2 * spacing_, height)];
        _date.font = [UIFont systemFontOfSize:height];
        _date.textColor = [UIColor lightGrayColor];
        _date.textAlignment = NSTextAlignmentRight;
        
    }
    return _date;
}

@end













